package com.gongpingjialoan.network;


import android.os.AsyncTask;

import com.gongpingjialoan.utility.HttpDownloader;

public class NetDownloadFile {

    public String SERVER_DOMAIN = "http://gongpingjia.qiniudn.com";
    public String API = "";

    public GetFileTask mTask;
    

    public OnNetDownloadFileListener mListener;

    public NetDownloadFile(OnNetDownloadFileListener listener){
        mListener = listener;
    }

    private class GetFileTask extends AsyncTask<String, String, Integer> {
        public String mPath;
        public String mFilename;
        public String mType;
        public String mId;

        protected Integer doInBackground(String... args) {
            if (5 != args.length){
                return -1;
            }
            HttpDownloader downloader = new HttpDownloader();
            mPath = args[1];
            mFilename = args[2];
            mType = args[3];
            mId = args[4];
            return downloader.downloadFile(args[0], mPath, mFilename);
        }

        protected void onProgressUpdate(String progress) {
        }

        protected void onPostExecute(Integer result) {
            if (0 == result) {
                mListener.onDownloadUpdate(mPath, mFilename, mType, mId);
            }
        }
    }

    public void requestData(String url, String path, String filename, String type, String id){
        API = url;
        mTask = new GetFileTask();
        mTask.execute(SERVER_DOMAIN + API, path, filename, type, id);
    }

    public void cancelTask(){
        if (null != mTask){
            mTask.cancel(true);
        }
    }
    

    public interface OnNetDownloadFileListener {
        public void onDownloadUpdate(String path, String filename, String type, String id);
    }
}
